package utility;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.w3c.dom.Document;

public class Util_Img {
	private static final String token = "AgAAAA**AQAAAA**aAAAAA**VP4pTQ**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6wFk4CoAJmGow+dj6x9nY+seQ**Y3IBAA**AAMAAA**4VIeJEk2tIexZ/PMGLr3CGDpY7CweeVJRaq1XimMp1Oxl0iPQsgHyFIFlSKYwlAPqYUiYUc1AY/1I9tjJOAKwrfbTdQqRSgc41XiYWqQK59f2RPPcOXSM2cRdJI3qIHiLU9v3rjcqgpo0sG7zIYw0M+2Amo48E0I2zr8XlKv2VQXg7rLRYZeXj7GnJYOpBg9o9CCQRoBT6kXYyVaKYpGKp/spcq004tHSB8TyhO3EkKsAJcLfmsGsxkWsb5Cxp0Sgn0oOAqYtwfbYIPQ4z+mlfLFi52Bbjem+LrsLgcEyfvksdL+83tqwzmZqrb41Bj0VBXD4aI9fxPdxK6XmObosUu8TtnGRh9X454gFgelqVugYJkqSjlvtiIn86uleNs+Y/Sid77cyyI7Fs2TvoYTQ2+rMMQLyR5iwZgIGoJW4XdDfLCkeyatWqeER7gcGkdQFf+revawSl6TngczvSQx1ZG6O9sgmq5uVE3WF1n1wQijz7qnxOSG39c78tzDpslYOdEfvbUajkDvFmkLBiMgXPRek1B9i73l7sVK6zobz0T8yDhk7Uk8R992zZNJckwF7IwwlxReELapwpcbVrQXy1LKiVxdUDxfV/2ctvoUtqWBY5h4UoHZEOpUjztRy1YwvqaessTcX5hmew8zuDeGWzod2ST4gkPDUg0uWdzhbQr1KEgFSnyrwNHi/Ixx1mesIteksNMG10D5DTQOoWzzglBCIvtCTHOT18qyPm6LABCAg+hUmx8rE5zboJ98jU8c";

	// input inputstream of image, upload it to ebay server, return img url.
	public static String upload(File file) {
		String rs = "";
		// get xml string
		String uploadPicturesXML = " <?xml version=\"1.0\" encoding=\"utf-8\"?> \n"
				+ "<UploadSiteHostedPicturesRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\"> \n\t"
				+ "<RequesterCredentials> \n\t\t"
				+ " <ebl:eBayAuthToken xmlns:ebl=\"urn:ebay:apis:eBLBaseComponents\">"
				+ token
				+ "</ebl:eBayAuthToken>\n\t\t"
				+ "</RequesterCredentials>\n\t"
				+ "<PictureName>testPic</PictureName>\n"
				+ "<PictureSet>Supersize</PictureSet>\n"
				+ "</UploadSiteHostedPicturesRequest>\n";

		// build Parts
		Part[] parts = new Part[2];
		parts[0] = new StringPart("XML Payload", uploadPicturesXML);
		try {
			parts[1] = new FilePart(file.getName(), file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		HttpClient client = new HttpClient();
		PostMethod filePost = new PostMethod(
				"https://api.sandbox.ebay.com/ws/api.dll");
		try {

			filePost.addRequestHeader("X-EBAY-API-CALL-NAME",
					"UploadSiteHostedPictures");
			// include both xml message and an attachment

			filePost.setRequestEntity(new MultipartRequestEntity(parts,
					filePost.getParams()));

			filePost.addRequestHeader("SOAPAction", "");
			filePost.addRequestHeader("X-EBAY-API-SESSION-CERTIFICATE",
					"Carnegie-4827-41e3-8147-bb6e505fd31e" + ";"
							+ "fd40c571-c3c5-455d-9ff1-82c284b29d80" + ";"
							+ "23ca5549-1e53-4540-accb-8bb2e878bc4b");
			filePost.addRequestHeader("X-EBAY-API-COMPATIBILITY-LEVEL", "685");
			filePost.addRequestHeader("X-EBAY-API-DEV-NAME",
					"fd40c571-c3c5-455d-9ff1-82c284b29d80");
			filePost.addRequestHeader("X-EBAY-API-APP-NAME",
					"Carnegie-4827-41e3-8147-bb6e505fd31e");
			filePost.addRequestHeader("X-EBAY-API-CERT-NAME",
					"23ca5549-1e53-4540-accb-8bb2e878bc4b");
			filePost.addRequestHeader("X-EBAY-API-SITEID", "0");
			filePost.addRequestHeader("X-EBAY-API-DETAIL-LEVEL", "0");
			// send messages
			int status = client.executeMethod(filePost);

			//System.out.println(filePost.getResponseBodyAsString());
			// get xml response to retreive image url
			InputStream in = filePost.getResponseBodyAsStream();
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder;
			docBuilder = factory.newDocumentBuilder();
			Document xmlDoc = docBuilder.parse(in);
            rs=Util_Trading.xmlGet(xmlDoc, "FullURL");
            filePost.releaseConnection();
            file.delete();
		} catch (Exception ex) {
			ex.toString();

		}

		
		return rs;
	}
}
